#include "drawsubway.h"
#include "Init.h"
#include <QGraphicsView>
#include <QDebug>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsPixmapItem>
#include "ZoomIO.h"
#include<qtextcodec.h>
void drawsys(QGraphicsView *graphicsView){

    QGraphicsScene *scene;   //绘图场景
    scene = new QGraphicsScene;
    scene->setBackgroundBrush(Qt::white);
    scene->setSceneRect(0,0,1500,1000);// f分辨率
    qDebug()<<"传参成功";
    graphicsView->setRenderHint(QPainter::Antialiasing);
    graphicsView->setScene(scene);
    graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);

    scene->clear();
  //  refresh(graphicsView);
    //test
//    QImage image(":/image/src/qdsubway.jpg");
//    QPixmap pixmap = QPixmap::fromImage(image);
//    // 创建一个 QGraphicsItem 对象，并将图片作为数据
//    QGraphicsItem *item = scene->addPixmap(pixmap);
//    // 调整 QGraphicsItem 的大小
//    QRectF rect(0, 0, 200, 100);
//  //  item->setSize(100,100);
//    //item->setPos(0,0);
//    scene->setSceneRect(rect);
//    return;

    QMap<QString,QMap<QString,int> >flag;
    flag.clear();
    for(auto i:Station.keys()){
        for(auto &j:edge[i]){
            QGraphicsLineItem *line = new QGraphicsLineItem();
            QPointF pos1,pos2;
            if(flag[i][j]) continue;
            flag[i][j]=flag[j][i]=1;
            pos1.setX(Station[i].coord.x()+3);
            pos1.setY(Station[i].coord.y()+3);
            pos2.setX(Station[j].coord.x()+3);
            pos2.setY(Station[j].coord.y()+3);
            line->setLine(QLineF(pos1,pos2));
            if(mp[i][j].size()==1){
                line->setPen(QPen(LineColor[*mp[i][j].begin()]));
            }else line->setPen(QPen(Qt::black));
            scene->addItem(line);
        }
    }
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    for(auto i:Station.keys()){

        QPen elli_pen;
   //     qDebug()<<"station linenum"<<Station_Line[i].size()<<"\n";
        if(Station_Line[i].size()!=1)
            elli_pen.setColor(Qt::black);
        else
            elli_pen.setColor(LineColor[*Station_Line[i].begin()]);
        QGraphicsEllipseItem *elli = new QGraphicsEllipseItem();
        elli->setRect(QRect(Station[i].coord.x(),Station[i].coord.y(),6,6));

        qDebug()<<i<<" "<<Station[i].coord.x()<<" "<<Station[i].coord.y()<<"\n";
     //   qDebug()<<*Station_Line[i].begin()<<" "<<LineColor[*Station_Line[i].begin()]<<" \n";

        elli->setPen(elli_pen);

        elli->setBrush(Qt::white);
        /*
        在这段代码中，您设置了两个颜色：一个是 QPen 的颜色，另一个是 QBrush 的颜色
        QPen 用于绘制形状的边框，而 QBrush 用于绘制形状的填充颜色。
        */
        //Latitude and longitude）即经线和纬线
        QString info = QString::fromUtf8("站点名称:");
        info+=i;
        info+=QString::fromUtf8("\n站点经纬度:");
        info+=QString::number(Station[i].e,'f',3);
        info+=",";
        info+=QString::number(Station[i].n,'f',3);
        info+=QString::fromUtf8("\n站点所属线路:");
        for(auto j:Station_Line[i].values()){
            info += j+' ';
        }
        info+=QString::fromUtf8("\n相邻站点");
        for(auto &j:edge[i]){
            info+="\n "+j;
            if(j.length()<=3) info+="    ";
            info+=QString::fromUtf8("\t相距")+QString::number(dp[i][j]/1000.0)+"km";
        }
        scene->addItem(elli);

        QGraphicsTextItem *text = new QGraphicsTextItem();
        text->setPlainText(i);
        text->setPos(Station[i].coord.x(),Station[i].coord.y()+1);
        text->setFont(QFont("consolas",4,1));
        text->setToolTip(info);
        scene->addItem(text);
    }
}

